package com.adong.dao.impl;

import com.adong.dao.RoleDao;
import com.adong.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author com.adong
 */

public class RoleDaoImpl implements RoleDao {


    JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override
    public List<Role> findAll() {
        String sql = "SELECT id,roleName,roleDesc FROM sys_role";
        List<Role> roleList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }

    @Override
    public void save(Role role) {
        String sql = "insert into sys_role values(?,?,?)";
        jdbcTemplate.update(sql, null, role.getRoleName(), role.getRoleDesc());

    }


    @Override
    public List<Role> findRoleByUserId(Long id) {
        String sql = "SELECT * FROM sys_user u,sys_role r,sys_user_role ur WHERE u.`id` = ur.`userId` AND r.`id` = ur.`roleId` AND  u.id=? ";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Role>(Role.class), id);
    }
}
